package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.console.excel.ExportExcel;
import com.qumitech.visualnovel.console.excel.State;
import com.qumitech.visualnovel.console.excel.StateField;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.controller.vo.voiceRoom.VoiceRoomStatisticSearchConditionDTO;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.ConsoleVoiceRoomDataDTO;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.query.VoiceRoomDataQueryDTO;
import com.qumitech.visualnovel.pubdef.dto.voiceroom.query.VoiceRoomUserUpperQueryDTO;
import com.qumitech.visualnovel.service.voiceRoom.VoiceRoomStatisticService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @Auther: yilu
 * @Date: 2019/3/12 16:28
 * @Description:
 */

@RestController
@RequestMapping("/api/voiceRoom/data")
public class VoiceroomStatisticController {
    @Autowired
    private VoiceRoomStatisticService voiceRoomStatisticService;

    @GetMapping
    @ExportExcel(listKey = "data", itemCls = ConsoleVoiceRoomDataDTO.class, stateFields = {
            @StateField(key="rebateType", states = {
                    @State(state = "1", display = "个人房"),
                    @State(state = "2", display = "娱乐房"),
                    @State(state = "3", display = "陪玩房"),
                    @State(state = "4", display = "电台房"),
                    @State(state = "5", display = "停止营业"),
                    @State(state = "6", display = "相亲房"),
                    @State(state = "8", display = "派单厅"),
            })
    })
    public BaseRsp getRoomData(VoiceRoomDataQueryDTO queryDTO) {

        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("data", voiceRoomStatisticService.getVoiceRoomData(queryDTO),
                "count", voiceRoomStatisticService.countVoiceRoomData(queryDTO),
                "statistic", voiceRoomStatisticService.getVoiceRoomStatisticData(queryDTO)));
    }

    @GetMapping(value = "/detail")
    public BaseRsp getRoomDetail(@RequestParam(name = "streamer_id") Integer streamerId,
                                 @RequestParam(name = "voice_room_id", required = false) String voiceRoomId,
                                 @RequestParam(name = "app_channel", required = false) String appChannel,
                                 @RequestParam(name = "streamer_system", required = false) String streamerSystem,
                                 @RequestParam(name = "startTime", required = false) String startTime,
                                 @RequestParam(name = "endTime", required = false) String endTime,
                                 @RequestParam(name = "limit", required = false, defaultValue = "0") Integer limit,
                                 @RequestParam(name = "offset", required = false, defaultValue = "20") Integer offset,
                                 @RequestParam(value = "orderType", required = false, defaultValue = "DESC") String orderType,
                                 @RequestParam(value = "orderBy", required = false, defaultValue = "streamer_id") String orderBy) {
        VoiceRoomStatisticSearchConditionDTO conditionDTO = new VoiceRoomStatisticSearchConditionDTO();
        conditionDTO.setVoiceRoomId(voiceRoomId);
        conditionDTO.setAppChannel(appChannel);
        conditionDTO.setStreamerId(streamerId);
        conditionDTO.setStreamerSystem(streamerSystem);
        conditionDTO.setStartTime(startTime);
        conditionDTO.setEndTime(endTime);
        conditionDTO.setLimit(limit);
        conditionDTO.setOffset(offset);
        conditionDTO.setOrderType(orderType);
        conditionDTO.setOrderBy(orderBy);
        return new BaseRsp(ResultEnum.SUCCESS, voiceRoomStatisticService.getVoiceRoomDetailData(conditionDTO));
    }

    @GetMapping(value = "/upper_voice_room")
    @ExportExcel(endTimeKey = "end_time", listKey = "data",
            itemKeys = {"targetDate", "streamerId", "accountId", "upperTime", "lowerTime", "duration","seatNo"},
            itemNames = {"统计日期","厅主ID", "上麦用户ID", "上麦时间","下麦时间","本次上麦时常/秒", "麦序"}
    )
    public BaseRsp getUserUpperVoiceRoom(VoiceRoomUserUpperQueryDTO voiceRoomUserUpperQueryDTO)
    {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("data", voiceRoomStatisticService.listUserUpperVoiceRoom(voiceRoomUserUpperQueryDTO),
                "count", voiceRoomStatisticService.countUserUpperVoiceRoom(voiceRoomUserUpperQueryDTO),
                "statistic", voiceRoomStatisticService.statisticUserUpperVoiceRoom(voiceRoomUserUpperQueryDTO)));
    }

    @GetMapping(value = "/daily_sum")
    public BaseRsp getRoomDataDailySum(VoiceRoomDataQueryDTO queryDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "items", voiceRoomStatisticService.getVoiceRoomDataDailySum(queryDTO),
                "count", voiceRoomStatisticService.countVoiceRoomDataDailySum(queryDTO)));
    }

    @GetMapping(value = "/pie")
    public BaseRsp getRoomDataPie(VoiceRoomDataQueryDTO queryDTO) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of(
                "items", voiceRoomStatisticService.getVoiceRoomDataPie(queryDTO)));
    }

    @GetMapping(value = "/line")
    public BaseRsp getRoomDataLine(VoiceRoomDataQueryDTO queryDTO) {
        JSONObject res;
        res = voiceRoomStatisticService.getVoiceRoomDataLine(queryDTO);
        return new BaseRsp(ResultEnum.SUCCESS, res);
    }

}
